﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;using System.Configuration;
using System.Windows.Forms;


namespace ChairtyBank.DAL
{
    public class UserEntity
    {
        public Int32 Id { get; set; }
        public String UName { get; set; }
        public String UPassword { get; set; }
        public String UType { get; set; }


    }
    class User
    {
        //Set absolute DB path rather than relative to solve openFileDialog reset path problem
        private static string conString = ConfigurationManager.ConnectionStrings["ChairtyBank"].ToString();
        private static string cmdText = "";

        public static String MASTER_USER_UType = "مدير عام";
        public static String ADMIN_USER_UType = "مدير";
        public static String NORMAL_USER_UType = "مستخدم عادي";
        public static String SEARCHER_USER_UTYPE = "باحث";


        public static UserEntity GetUser(String Name)
        {
            UserEntity c = null;

            cmdText = "select * from PropUsers where UName=@uname";
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@uname", Name);
            try
            {
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    c = fillEntityFromReader(reader);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("UserEntity.GetUser()==>>" + ex.Message);
            }

            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return c;
        }


        public static UserEntity GetUser(int id)
        {
            UserEntity c = new UserEntity();

            cmdText = "select * from PropUsers where Id=@Id";
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Id", id);
            try
            {
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    c = fillEntityFromReader(reader);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("UserEntity.GetUser()==>>" + ex.Message);
            }

            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return c;
        }


        public static List<UserEntity> GetAllUser()
        {
            UserEntity c = null;
            List<UserEntity> Users = new List<UserEntity>();

            cmdText = "select * from PropUsers";
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);

            try
            {
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    c = fillEntityFromReader(reader);
                    Users.Add(c);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("UserEntity.GetAllUsers()==>>" + ex.Message);
            }

            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return Users;

        }

        public static bool Add(UserEntity c)
        {
            bool flag = true;
            cmdText = "insert into PropUsers (UName,UPassword,UType)values(@UName,@UPassword,@UType)";

            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@UName", c.UName);
            cmd.Parameters.AddWithValue("@UPassword", c.UPassword);
            cmd.Parameters.AddWithValue("@UType", c.UType);

            try
            {
                con.Open();
                Int32 numOfRow = cmd.ExecuteNonQuery();
                if (numOfRow == 0)
                    flag = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show("خطأ فى البيانات " + ex.Message);
                flag = false;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return flag;

        }

        public static bool Update(UserEntity c)
        {
            Boolean flag = false;
            cmdText = "UPDATE PropUsers SET UName=@UName,UPassword=@UPassword,UType=@UType WHERE Id=@Id";

            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@UName", c.UName);
            cmd.Parameters.AddWithValue("@UPassword", c.UPassword);
            cmd.Parameters.AddWithValue("@UType", c.UType);
            cmd.Parameters.AddWithValue("@Id", c.Id);
            try
            {
                con.Open();
                Int32 numOfRow = cmd.ExecuteNonQuery();
                flag = numOfRow > 0? true:false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("UserEntity.Update==>>" + ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();
            }

            return flag;
        }


        /*
           Delete using id or Serial 
        */

        public static bool Delete(Int32 c)
        {
            Boolean flag = false;
            cmdText = "Delete from PropUsers WHERE Id=@Id";


            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand(cmdText, con);
            cmd.Parameters.AddWithValue("@Id", c);
            try
            {
                con.Open();

                Int32 numOfRow = cmd.ExecuteNonQuery();
                flag = true;


            }
            catch (Exception ex)
            {
                MessageBox.Show("UserEntity.Delete==>>" + ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                    con.Close();

            }
            return flag;
        }




        private static UserEntity fillEntityFromReader(SqlDataReader reader)
        {
            UserEntity c = new UserEntity();
            c.Id = Int32.Parse(reader["Id"].ToString());

            if (reader["UName"] != null)
                c.UName = reader["UName"].ToString();
            if (reader["UType"] != null)
                c.UType = reader["UType"].ToString();
             if (reader["UPassword"] != null)
                c.UPassword = reader["UPassword"].ToString();
              
            return c;
        }


    }
}